/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.DailyReportDetail_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class DailyReportDetail_BLL {
    ConnectUnit connection;
    
    public DailyReportDetail_BLL(){
        this.connection= DAL.DAL.getDAL();
    }
    /**
     * get list of DailyReportDetails form database with Condition and OrderBy
     * @param condition
     * @param OrderBy
     * @return
     * @throws Exception 
     */
    public HashSet<DailyReportDetail_DTO> getDailyReportDetails (String condition, String OrderBy) throws Exception{
        ResultSet result = this.connection.Select("DAILYREPORTDETAIL", condition, OrderBy);
        HashSet<DailyReportDetail_DTO> dailyRDs = new HashSet<DailyReportDetail_DTO>();
        while(result.next()){
            DailyReportDetail_DTO dailyRD = new DailyReportDetail_DTO();
            dailyRD.setDetailID(result.getString("DetailID"));
            dailyRD.setReportID(result.getString("ReportID"));
            dailyRD.setSavingTypeID(result.getString("SavingTypeID"));
            dailyRD.setTotalOfCost(result.getByte("TotalOfCost"));
            dailyRD.setTotalOfRevenues(result.getByte("TotalOfRevenues"));
            
            dailyRDs.add(dailyRD);
        }
        return dailyRDs;        
    }
    
    /**
     * get list of DailyReportDetails form database with Condition
     * @param condition
     * @return
     * @throws Exception 
     */
    public HashSet<DailyReportDetail_DTO> getDailyReportDetails (String condition) throws Exception{
        return this.getDailyReportDetails(condition, null);
    } 
    
    /**
     * get list of DailyReportDetails form database
     * @return
     * @throws Exception 
     */
    public HashSet<DailyReportDetail_DTO> getDailyReportDetails () throws Exception{
        return this.getDailyReportDetails(null, null);
    } 
    
    /**
     * get DailyReportDetail by DetailID
     * @param ID
     * @return
     * @throws Exception 
     */
    public DailyReportDetail_DTO getDailyReportDetailBy_DetailID(String ID) throws Exception{
        HashSet<DailyReportDetail_DTO> dailyRD= this.getDailyReportDetails("DetailID='"+ ID+"'");
        return (dailyRD.size()>0) ? dailyRD.toArray(new DailyReportDetail_DTO[dailyRD.size()])[0] : null;        
    }
    
    /**
     * get DailyReportDetail by Index
     * @param list
     * @param index
     * @return 
     */
    public DailyReportDetail_DTO getDailyReportDetailByIndex(HashSet<DailyReportDetail_DTO> list, int index){
        return (index >= list.size()) ? null : list.toArray(new DailyReportDetail_DTO[list.size()])[index];
    }
    
    /**
     * Insert DailyReportDetail into database 
     * @param info
     * @throws Exception 
     */
    public void Insert(DailyReportDetail_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("DetailID", info.getDetailID());
        map.put("ReportID", info.getReportID());
        map.put("SavingTypeID", info.getSavingTypeID());
        map.put("TotalOfCost", info.getTotalOfCost());
        map.put("TotalOfRevenues", info.getTotalOfRevenues());
        
        String[] arr = {"DetailID","ReportID", "SavingTypeID","TotalOfCost","TotalOfRevenues"};
        this.connection.Insert("DAILYREPORTDETAIL", map, arr);
    }
    
    /**
     * Update DailyReportDetail into Database
     * @param info
     * @throws Exception 
     */
    public void Update(DailyReportDetail_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("DetailID", info.getDetailID());
        map.put("ReportID", info.getReportID());
        map.put("SavingTypeID", info.getSavingTypeID());
        map.put("TotalOfCost", info.getTotalOfCost());
        map.put("TotalOfRevenues", info.getTotalOfRevenues());
        
        this.connection.Update("DAILYREPORTDETAIL", map, "DetailID="+info.getDetailID());
    }
    
    /**
     * Delete DailyReportDetail in database
     * @param info
     * @throws Exception 
     */
    public void Delete(DailyReportDetail_DTO info) throws Exception{
        this.connection.Delete("DAILYREPORTDETAIL", "DetailID="+info.getDetailID());
    }
}
